package com.culturalCenter.dataCenter.service.impl.activity;

import cn.hutool.core.util.ObjectUtil;
import com.culturalCenter.dataCenter.customEntity.activity.CustomActivityException;
import com.culturalCenter.dataCenter.customEntity.activity.CustomActivityExceptionIsExist;
import com.culturalCenter.dataCenter.customEntity.activity.CustomActivityVerifyTable;
import com.culturalCenter.dataCenter.customEntity.responseParam.activity.ActivityTimeLineResponse;
import com.culturalCenter.dataCenter.customEntity.training.CustomAttendanceList;
import com.culturalCenter.dataCenter.customEntity.training.CustomRegistrationList;
import com.culturalCenter.dataCenter.customEntity.training.CustomSignInList;
import com.culturalCenter.dataCenter.entity.activity.*;
import com.culturalCenter.dataCenter.mapper.activity.*;
import com.culturalCenter.dataCenter.service.activity.ActivityTimeLineService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : weicai
 * @date : 17:18 2020/11/2
 */
@Service
public class ActivityTimeLineServiceImpl implements ActivityTimeLineService {

    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private ActivityCreatorInfoMapper activityCreateInfoMapper;
    @Resource
    private ActivityAuditRecordMapper activityAuditRecordMapper;
    @Resource
    private ActivitySignUpRecommendMapper activitySignUpRecommendMapper;
    @Resource
    private ActivityDataStatisticsMapper activityDataStatisticsMapper;
    @Resource
    private ActivityMessagePushCountMapper activityMessagePushCountMapper;
    @Resource
    private ActivityInfoCountMapper activityInfoCountMapper;
    @Resource
    private ActivitySiteSituationMapper activitySiteSituationMapper;
    @Resource
    private ActivityApplyMapper activityApplyMapper;
    @Resource
    private ActivityMessagePushRecordMapper activityMessagePushRecordMapper;
    @Resource
    private ActivitySignInMapper activitySignInMapper;
    @Resource
    private ActivityAttendanceMapper activityAttendanceMapper;
    @Resource
    private ActivitySiteMonitorMapper activitySiteMonitorMapper;

    @Override
    public ActivityTimeLineResponse getTimeLine(String activityId) {
        ActivityTimeLineResponse activityTimeLineResponse = new ActivityTimeLineResponse();
        // 时间线
        Activity activity = activityMapper.queryTimeById(activityId);
        if (ObjectUtil.isNotEmpty(activity)) {
            activityTimeLineResponse.setCreateTime(activity.getCreateTime());
            activityTimeLineResponse.setSignUpBeginTime(activity.getSignUpBeginTime());
            activityTimeLineResponse.setSignUpEndTime(activity.getSignUpEndTime());
            activityTimeLineResponse.setSignInTime(activity.getSignInBeginTime());
            activityTimeLineResponse.setActivityBeginTime(activity.getBeginTime());
            activityTimeLineResponse.setActivityEndTime(activity.getEndTime());
        }
        // 创建人信息
        List<ActivityCreatorInfo> creatorInfoList = activityCreateInfoMapper.queryByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(creatorInfoList)) {
            activityTimeLineResponse.setCreatorInfoList(creatorInfoList);
        }
        // 审核人信息
        List<ActivityAuditRecord> auditList = activityAuditRecordMapper.queryByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(auditList)) {
            activityTimeLineResponse.setAuditSituation(auditList);
        }
        // 报名结束列表
        List<ActivityInfoCount> collectList = activityInfoCountMapper.querySignUpInfoByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(collectList)) {
            activityTimeLineResponse.setCollectList(collectList);
        }
        // 签到和考勤列表（活动开始）
        List<ActivityInfoCount> signInAndAttendanceList = activityInfoCountMapper.querySignInAndAttendance(activityId);
        if (CollectionUtils.isNotEmpty(signInAndAttendanceList)) {
            activityTimeLineResponse.setSignInAndAttendanceList(signInAndAttendanceList);
        }
        // 现场列表（活动结束）
        List<CustomActivityException> siteSituationList = activitySiteMonitorMapper.queryExceptionByActivityId(activityId);
        CustomActivityExceptionIsExist customActivityExceptionIsExist = new CustomActivityExceptionIsExist();
        if (CollectionUtils.isNotEmpty(siteSituationList)) {
            siteSituationList.forEach(w -> {
                if (w.getSituationType().contains("聚集")) {
                    customActivityExceptionIsExist.setIsExistGather("有");
                }
                if (w.getSituationType().contains("倒地")) {
                    customActivityExceptionIsExist.setIsExistFall("有");
                }
            });
            if (customActivityExceptionIsExist.getIsExistFall() == null) {
                customActivityExceptionIsExist.setIsExistFall("无");
            }
            if (customActivityExceptionIsExist.getIsExistGather() == null) {
                customActivityExceptionIsExist.setIsExistGather("无");
            }
        }
        activityTimeLineResponse.setSiteSituation(customActivityExceptionIsExist);
        // 回顾
        List<Activity> reviewList = activityMapper.queryReviewByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(reviewList)) {
            activityTimeLineResponse.setReviewList(reviewList);
        }

        return activityTimeLineResponse;
    }

    @Override
    public CustomActivityVerifyTable getVerifyTable(String activityId) {
        CustomActivityVerifyTable customActivityVerifyTable = new CustomActivityVerifyTable();

        List<ActivityAuditRecord> auditList = activityAuditRecordMapper.queryByActivityId(activityId);
        customActivityVerifyTable.setAuditList(auditList);
        List<Activity> activityInfoList = activityMapper.getActivityInfo(activityId);
        customActivityVerifyTable.setActivityInfoList(activityInfoList);

        return customActivityVerifyTable;
    }

    @Override
    public List<CustomRegistrationList> getRegistrationList(String activityId) {
        return activityApplyMapper.selectRegistration(activityId);
    }

    @Override
    public List<CustomSignInList> getSignInList(String activityId) {
        return activitySignInMapper.selectSignInList(activityId);
    }

    @Override
    public List<CustomAttendanceList> getAttendanceList(String activityId) {
        return activityAttendanceMapper.selectAttendanceList(activityId);
    }

    @Override
    public List<ActivitySiteMonitor> getMonitorPicture(String activityId) {
        return activitySiteMonitorMapper.selectPicture(activityId);
    }

}
